﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Message.SMS.SGIP
{
    public abstract class RespCommand : Command
    {
        private ErrorCode _Result = ErrorCode.Success;
        /// <summary>
        /// Bind执行命令是否成功。
        /// </summary>
        public ErrorCode Result
        {
            get { return _Result; }
            set { _Result = value; }
        }

        private string _Reserve = "";

        public string Reserve
        {
            get { return _Reserve; }
            set { _Reserve = value; }
        }

        #region 构造函数
        public RespCommand()
        {
        }

        public RespCommand(byte[] bytes)
            : base(bytes)
        {
        }

        public RespCommand(byte[] bytes, int startIndex)
            : base(bytes, startIndex)
        {
        }

        public RespCommand(CommandSequence sequence)
            : base(sequence)
        {
        }
        #endregion

        public override byte[] ToBytes()
        {
            byte[] bytes = base.ToBytes();
            int i = (int)CommandHeader.HeaderLength;
            bytes[i] = (byte)Result;
            i++;
            ByteStringConvert.String2Bytes(_Reserve.PadRight(8, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            return bytes;
        }

        protected override void BytesTo(byte[] bytes, int startIndex)
        {
            base.BytesTo(bytes, startIndex);
            int i = (int)CommandHeader.HeaderLength;
            Result = (ErrorCode)bytes[i];
            i++;
            Reserve = ByteStringConvert.Bytes2String(bytes, i, 8, MessageCoding.ASCII).TrimEnd('\0');
        }
    }
}
